#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h> 
#include <stdlib.h>
#include <assert.h>
#include "app_platform/cntl_info.h"

#include "gismanageao.h"
#include "gismanageao_enum.h"
#include "erp/gis/po/shiptypearealocuspo_gis.h"
#include "erp/gis/ddo/shiptypearealocusdo_gis.h"
#include "erp/gis/ddo/shiptypearealocusdolist_gis.h"
#include "erp/gis/dao/giserpdao_stub4app_allinone.h"
#include "erp/gis/to/shiptypearealocuspo_conv_shiptypearealocusdo.h"
#include "libc2c/c2c_errno_define.h"
#include "erp/comm/erp_macro_define.h"

using namespace std;
using namespace c2cent::library::wwlog;
using namespace c2cent::library::wtimeelf;
using namespace erp::gis::ao;

CPP_SELF_DEFINE_START
/**
*  请在这里增加自定义的成员变量和函数及其他内容，否则无法合并代码
**/
c2cent::library::wtimeelf::CItilGroup CGisManageAo::m_oShipTypeAreaLocusRowSync(ITIL_SHIPTYPEAREALOCUS_ROW_SYNC_REQ, ITIL_SHIPTYPEAREALOCUS_ROW_SYNC_ERR, ITIL_SHIPTYPEAREALOCUS_ROW_SYNC_RSP, ITIL_SHIPTYPEAREALOCUS_ROW_SYNC_DEF,c2cent::library::wtimeelf::TIME_50MS);

CPP_SELF_DEFINE_END

/**
 *构建函数
 */
CGisManageAo::CGisManageAo(enum DeployMethod method)
{

}

/**
 *析构函数
 */
CGisManageAo::~CGisManageAo()
{

}


/**
 *每一次服务请求时，container都会调用此方法来清空数据
 */
void  CGisManageAo::Reset()
{

}


int  CGisManageAo::Initialize(IUserDefineCntl* pCntl)
{
	int iRet = 0;
	uint32_t dwLogSwitch = WLOG_OPEN|WLOG_KEY_OPEN|WLOG_ERR_OPEN;
	uint32_t dwCanLogRemote = 0;
	std::string strLogModule="ao_gismanager";


	std::cout<<"File:"<<__FILE__<<" Complie Date: "<<__DATE__<<" "<<__TIME__<<std::endl;

	GetUintConfig(LogSwitch);
	GetStringConfig(LogModule);
	GetUintConfig(CanLogRemote);
	// 初始化日志
	if(  C2C_WWLOG->QuickInitForAPP2(strLogModule.c_str(), B2B2CWW_LOG_APP_FILE_PATH, dwCanLogRemote, dwLogSwitch) != 0)
	{
		std::cerr << "C2C_WWLOG->QuickInitForAPP2 Failed" << std::endl;
		return -1;
	}

	C2C_WW_LOG("C2C_WWLOG->QuickInitForAPP2 OK!");

	return iRet;
}


/**
 *
 * 同步配送区域轨迹记录 
 *
 *@param String machineKey: 机器码，必填，对应网站cookie中的visitkey，若无则填随机值  [in]				
 *@param String source: 调用来源文件名称，必填  [in]				
 *@param uint32_t sceneId: 场景id，必填（目前填0即可）  [in]				
 *@param ShipTypeAreaLocusPo shipTypeAreaLocusPo: 配送区域轨迹PO  [in]				
 *@param String inReserve: 请求保留字段  [in]				
 *@param String errMsg: 错误信息 	[out]	
 *@param String outReserve: 请求保留字段 	[out]	
 *
 *@return  uint32_t : 0(成功) 其它失败
 *
 */
uint32_t CGisManageAo::ShipTypeAreaLocusRowSync(
				const CCntlInfo& rCntlInfo,
				const std::string& strMachineKey,
				const std::string& strSource,
				uint32_t dwSceneId,
				const erp::gis::po::CShipTypeAreaLocusPo& oShipTypeAreaLocusPo,
				const std::string& strInReserve,
				std::string& strErrMsg,
				std::string& strOutReserve
	)
{
	int dwRet = 0;
	C2C_WW_SIM_LOG_PL_HEAD();
	C2C_TIME_ELF_ITIL oWTimeElf(C2C_WWLOG, __FUNCTION__, &dwRet, &m_oShipTypeAreaLocusRowSync);
	std::stringstream oss;
	oShipTypeAreaLocusPo.DumpData(oss);
	C2C_WW_LOG("GetAReq %s CmdId:%u strMachineKey:%s strSource:%s dwSceneId:%u,dumpFliter:%s",
				__FUNCTION__, rCntlInfo.getCmdId(), strMachineKey.c_str(), strSource.c_str(), dwSceneId,oss.str().c_str());

    if(_CheckCommParameter(rCntlInfo,strMachineKey,strSource,dwSceneId) != 0
            || _ValidateShipTypeAreaLocusPo(oShipTypeAreaLocusPo) != 0)
    {
		C2C_WW_LOG_ERR(ERR_APP_PARAM_INVALID, "Parameter invalid, CmdId:%u strMachineKey:%s strSource:%s dwSceneId:%u dumpFliter:%s",
				rCntlInfo.getCmdId(), strMachineKey.c_str(), strSource.c_str(), dwSceneId, oss.str().c_str());
        return ERR_APP_PARAM_INVALID;
    }
    
    erp::gis::ddo::CShipTypeAreaLocusDo oShipTypeAreaLocusDo;
    erp::gis::to::CShipTypeAreaLocusPoConvCShipTypeAreaLocusDo::Conv_CShipTypeAreaLocusPo_To_CShipTypeAreaLocusDo(oShipTypeAreaLocusPo, oShipTypeAreaLocusDo);

    erp::gis::dao::CGisErpDaoStub4App oGisErpDaoStub4App;
    dwRet = oGisErpDaoStub4App.ShipTypeAreaLocusRowSync(
				rCntlInfo,
				strMachineKey,
				strSource,
				dwSceneId,
				oShipTypeAreaLocusDo,
				strInReserve,
				strErrMsg,
				strOutReserve
            );

    if(dwRet != 0)
    {
		C2C_WW_LOG_ERR(dwRet,"Call ShipTypeAreaLocusRowSync falied. CmdId:%u strMachineKey:%s strSource:%s dwSceneId:%u, dumpFliter:%s, ErrMsg:(%s)",
		rCntlInfo.getCmdId(), strMachineKey.c_str(), strSource.c_str(), dwSceneId, oss.str().c_str(), strErrMsg.c_str());
		C2C_WW_LOG_LAST_ERR_LOG(strErrMsg);
		return dwRet;
    }

	C2C_WW_LOG("%s Finished, CmdId:%u strMachineKey:%s strSource:%s dwSceneId:%u",
				__FUNCTION__, rCntlInfo.getCmdId(), strMachineKey.c_str(), strSource.c_str(), dwSceneId);
	
	return dwRet;
}


uint32_t CGisManageAo::_ValidateShipTypeAreaLocusPo(const erp::gis::po::CShipTypeAreaLocusPo& oShipTypeAreaLocusPo)
{
    if(oShipTypeAreaLocusPo.GetShipTypeLocusGbAreaid() == 0)
    {
        C2C_WW_LOG_ERR(ERR_APP_PARAM_INVALID, "ShipTypeLocusGbAreaid can't be 0");
		return ERR_APP_PARAM_INVALID;
    }
    
    return 0;
}

uint32_t CGisManageAo::_CheckCommParameter(
		 const CCntlInfo& rCntlInfo,
		 const std::string& strMachineKey,
		 const std::string& strSource,
		 uint32_t dwSceneId
         )
{
    uint32_t dwRet = 0;
	if (strSource.empty())
	{
		C2C_WW_LOG_ERR(ERR_APP_PARAM_INVALID, "strSource need set!");
		return ERR_APP_PARAM_INVALID;
	}

	if (strMachineKey.empty())
	{
		C2C_WW_LOG_ERR(ERR_APP_PARAM_INVALID, "strMachine need set!");
		return ERR_APP_PARAM_INVALID;
	}
    
    return dwRet;
}

